from plugins.template import ParserTemplate
import re

def get_parsers():
	return {"Raze" : Parser}

class Parser(ParserTemplate):
	"""
	Grammar:
	Page = "Page" Number NL .
	Text = Translation NL.

	Remove (.*), remove [ and ] (but keep things in between!)
	double bubbles, etc. are split by two tabs
	"""

	_PageRe = re.compile(r'''Page\ (\d+) 	# Page<blank><Number>
	$ # end''', re.VERBOSE | re.IGNORECASE)
	
	def __init__(self, file):
		super().__init__(file)
	
	def _parse_next(self, last_token):
		try:
			page = self._parse_re(self._PageRe) or last_token.page
			panel = "?"
			line = self._cur_line
			texts = self._parse_texts()
			return [self.Token(page, panel, text, line) for text in texts]
		except self.ParserEndError:
			return [None]

	def _parse_texts(self):
		text = self._get_line()
		self._next_line()
		text = re.sub(r"\[|]|\(.*\)", "", text)
		return text.split("\t\t")
